package com.shop.shop.utile;

import cn.hutool.core.img.ImgUtil;
import cn.hutool.extra.qrcode.QrCodeUtil;
import com.alipay.api.AlipayApiException;
import com.alipay.api.AlipayClient;
import com.alipay.api.DefaultAlipayClient;
import com.alipay.api.request.AlipayTradePrecreateRequest;
import com.alipay.api.request.AlipayTradeQueryRequest;
import com.alipay.api.response.AlipayTradePrecreateResponse;
import com.alipay.api.response.AlipayTradeQueryResponse;
import com.shop.shop.config.PayConfig;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.context.annotation.Configuration;
import org.springframework.stereotype.Component;
import org.springframework.stereotype.Controller;

import javax.servlet.http.HttpServletResponse;
import java.io.IOException;

/**
 * 支付工具
 */

public class PayUtile {

    private static AlipayClient alipayClient = new DefaultAlipayClient(PayConfig.URL, PayConfig.APP_ID, PayConfig.APP_PRIVATE_KEY, PayConfig.FORMAT, "utf-8", PayConfig.ALIPAY_PUBLIC_KEY, PayConfig.SIGN_TYPE);


    public static AlipayClient getAlipayClient() {
        return alipayClient;
    }

    /**
     *  获取支付二维码
     * @param httpResponse
     * @param order_id 订单号
     * @param price  价格
     * @param productName  产品名
     * @param paymentTime  下单最晚时间
     */
    public static void getPay(HttpServletResponse httpResponse,String order_id,Double price,String productName,Integer paymentTime) {
        AlipayTradePrecreateRequest request = new AlipayTradePrecreateRequest(); //创建API对应的request类
        request.setBizContent("{" +
                "\"out_trade_no\":\""+order_id+"\"," + //商户订单号
                "\"total_amount\":\""+price+"\"," +
                "\"subject\":\""+productName+"\"," +
                "\"store_id\":\"NJ_001\"," +
                "\"timeout_express\":\""+paymentTime+"m\"}"); //订单允许的最晚付款时间
        AlipayTradePrecreateResponse response = null;
        try {
            response = alipayClient.execute(request);
        } catch (AlipayApiException e) {
            e.printStackTrace();
        }
        //  System.out.println(response.getQrCode());
        try {
            QrCodeUtil.generate(response.getQrCode(), 300, 300, ImgUtil.IMAGE_TYPE_JPEG, httpResponse.getOutputStream());
        } catch (IOException e) {

        }
    }

    /**
     *  获取支付状态
     * @param order_id 订单号
     * @return
     */
     public static Double getPaymentSuccess(String order_id){
         AlipayTradeQueryRequest request1 = new AlipayTradeQueryRequest();//创建API对应的request类
         request1.setBizContent("{" +
                 "\"out_trade_no\":\""+order_id+"\"," +
                 "      \"query_options\":[" +
                 "        \"trade_settle_info\"" +
                 "      ]" +
                 "  }");
         AlipayTradeQueryResponse response1 = null;//通过alipayClient调用API，获得对应的response类
         try {
             response1 = alipayClient.execute(request1);
         } catch (AlipayApiException e) {

         }
         //获取支付的状态码 10000 已经预支付  不知道是否成功支付
         int i = Integer.parseInt(response1.getCode());
         //获取支付的金额
         double amount = Double.parseDouble(response1.getBuyerPayAmount());
       if (i==10000){
            if(amount>0){
                 return amount;
            }
       }
       return null;
    }

}
